package com.ruoyi.smart.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.smart.service.DeviceStatusService;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 设备状态接口
 */
@RestController
@RequestMapping("/smart/device/status")
public class DeviceStatusController extends BaseController {

    private final DeviceStatusService deviceStatusService;

    public DeviceStatusController(DeviceStatusService deviceStatusService) {
        this.deviceStatusService = deviceStatusService;
    }

    /**
     * 获取所有设备状态
     */
    @GetMapping("/list")
    public AjaxResult getAllDeviceStatus() {
        return AjaxResult.success(deviceStatusService.getAllDeviceStatus());
    }

    /**
     * 获取指定设备状态
     */
    @GetMapping("/{deviceId}")
    public AjaxResult getDeviceStatus(@PathVariable String deviceId) {
        return AjaxResult.success(deviceStatusService.getDeviceStatus(deviceId));
    }

    /**
     * 获取设备统计信息
     */
    @GetMapping("/statistics")
    public AjaxResult getDeviceStatistics() {
        return AjaxResult.success(deviceStatusService.getDeviceStatistics());
    }

    /**
     * 清理过期设备
     */
    @PostMapping("/cleanup")
    public AjaxResult cleanupExpiredDevices(@RequestParam(defaultValue = "86400000") long expiryTime) {
        deviceStatusService.cleanupExpiredDevices(expiryTime);
        return AjaxResult.success("清理完成");
    }

    /**
     * 获取设备最后活动时间
     */
    @GetMapping("/lastActive/{deviceId}")
    public AjaxResult getDeviceLastActive(@PathVariable String deviceId) {
        return AjaxResult.success(deviceStatusService.getDeviceLastActive(deviceId));
    }

    /**
     * 移除设备状态信息
     */
    @DeleteMapping("/{deviceId}")
    public AjaxResult removeDevice(@PathVariable String deviceId) {
        deviceStatusService.removeDevice(deviceId);
        return AjaxResult.success("设备已移除");
    }
}
